package com.netjava.task;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import com.netjava.dao.IAucRecordDAO;
import com.netjava.dao.IAuctioningDAO;
import com.netjava.dao.IEndAuctionDAO;
import com.netjava.dao.IPreauctionDAO;
import com.netjava.dao.IProductDAO;
import com.netjava.dao.IUserDAO;
import com.netjava.pojo.AucRecord;
import com.netjava.pojo.Auctioning;
import com.netjava.pojo.EndAuction;
import com.netjava.pojo.Preauction;
import com.netjava.pojo.Product;

public class ScheduleTask extends TimerTask {
	private IAuctioningDAO auctioningDao;
	  private IEndAuctionDAO endauctionDao;
	  private IAucRecordDAO aucRecordDao;
	  private IProductDAO productDao;
	  private IPreauctionDAO preauctionDao;
	  private IUserDAO userDao;
	  private static boolean isRunning=false;    
	  
	  public   ScheduleTask(){         
	  }    
	  public void run(){  
		  List<Auctioning> auctionList=new ArrayList<Auctioning>();
		  auctionList=auctioningDao.findAll();                   
	  if(!isRunning){
		  isRunning   =   true; 
		  int i=0;
		  for(Auctioning auc:auctionList){
			  String remainTime=auc.getRemainTime();
			  Product product=auc.getProduct();
			  
			  Integer hour=Integer.parseInt(remainTime.substring(0,2).toString());
			  Integer min=Integer.parseInt(remainTime.substring(3,5).toString());
			  Integer second=Integer.parseInt(remainTime.substring(6,8).toString());
			  if(0!=second){
				  second=second-1;
			  }else{
				  if(0!=min){
					  min=min-1;
					  second=59;
				  }else{
					  hour=hour-1;
					  min=59;
					  second=59;
				  }
			  }
			  String hours=(hour<10)?("0"+hour.toString()+":"):hour.toString()+":";
			  String mins=(min<10)?("0"+min.toString()+":"):min.toString()+":";
			  String seconds=(second<10)?("0"+second.toString()):second.toString();
			  remainTime=hours+mins+seconds;
			  
			  
			  if(remainTime.equalsIgnoreCase("00:00:00")){
				  /**
				   * 将此商品从正在竞拍的商品中放入竞拍结束的商品中，
				   * 1.从正在竞拍的商品中删除此商品，
				   * 2.在竞拍结束的商品中添加此商品
				   * 3.修改商品表中该商品的状态为2
				   */
				  product.setState(2);
				  productDao.attachDirty(product);
				  
				  EndAuction endAuction=new EndAuction();
				  endAuction.setDeliverState(0);
				  endAuction.setPayTime("");
				  endAuction.setEndAuctionId(product.getProductId());
				  endAuction.setProduct(product);
				  List<AucRecord> aucRecordList=aucRecordDao.findByProId(product);
				  if(aucRecordList.size()!=0){
				  AucRecord aucRecord=aucRecordList.get(0);	 
				  endAuction.setEndTime(aucRecord.getAucTime());
				  endAuction.setUser(aucRecord.getUser());
				  }else{
					  endAuction.setEndTime("--:--:--");
					  endAuction.setUser(userDao.findById(1));
				  }
				  endauctionDao.save(endAuction);
				  
				  auctioningDao.delete(auc);
				 
				
				  /**
				   * 从即将竞拍的商品中取出一商品放入正在竞拍的商品中，
				   * 1.从即将竞拍的商品中删除此商品，
				   * 2.在正在竞拍的商品中添加此商品
				   * 3.修改商品表中该商品的状态为1
				   */
				  List<Preauction> preAucList=preauctionDao.getAllByASC();
				  if(preAucList.size()!=0){
				  Preauction preauction=preAucList.get(0);
				  Product pro=preauction.getProduct();
				  
				  Auctioning auctioning=new Auctioning();
				  auctioning.setAuctioningId(pro.getProductId());
				  auctioning.setProduct(pro);
				  auctioning.setRemainTime("24:00:00");
				  auctioningDao.save(auctioning);
				  
				  pro.setState(1);
				  productDao.attachDirty(pro);
				  
				  preauctionDao.delete(preauction);
				  }

				  
			  }
			  else{
				  auc.setRemainTime(remainTime);
				  auctioningDao.attachDirty(auc); 
			  }
			  i=i+1;
		  }
		  isRunning   =   false;    
	  }
	    else   {    
               System.out.println("上一次任务还没结束！");
	    }    
	  /**
	   * 当auctionList得长度小于4时，从即将竞拍的商品中取出一个放入正在竞拍的商品中
	   */
	  if(null!=auctionList&&auctionList.size()<4){
		  /**
		   * 从即将竞拍的商品中取出一商品放入正在竞拍的商品中，
		   * 1.从即将竞拍的商品中删除此商品，
		   * 2.在正在竞拍的商品中添加此商品
		   * 3.修改商品表中该商品的状态为1
		   */
		  List<Preauction> preAucList=preauctionDao.getAllByASC();
		  if(preAucList.size()!=0){
		  Preauction preauction=preAucList.get(0);
		  Product pro=preauction.getProduct();
		  
		  Auctioning auctioning=new Auctioning();
		  auctioning.setAuctioningId(pro.getProductId());
		  auctioning.setProduct(pro);
		  auctioning.setRemainTime("24:00:00");
		  auctioningDao.save(auctioning);
		  
		  pro.setState(1);
		  productDao.attachDirty(pro);
		  
		  preauctionDao.delete(preauction);
		  }
		  
	  }
	}
	public IAuctioningDAO getAuctioningDao() {
		return auctioningDao;
	}
	public void setAuctioningDao(IAuctioningDAO auctioningDao) {
		this.auctioningDao = auctioningDao;
	}
	public IEndAuctionDAO getEndauctionDao() {
		return endauctionDao;
	}
	public void setEndauctionDao(IEndAuctionDAO endauctionDao) {
		this.endauctionDao = endauctionDao;
	}
	public IAucRecordDAO getAucRecordDao() {
		return aucRecordDao;
	}
	public void setAucRecordDao(IAucRecordDAO aucRecordDao) {
		this.aucRecordDao = aucRecordDao;
	}
	public IProductDAO getProductDao() {
		return productDao;
	}
	public void setProductDao(IProductDAO productDao) {
		this.productDao = productDao;
	}
	public IPreauctionDAO getPreauctionDao() {
		return preauctionDao;
	}
	public void setPreauctionDao(IPreauctionDAO preauctionDao) {
		this.preauctionDao = preauctionDao;
	}
	public IUserDAO getUserDao() {
		return userDao;
	}
	public void setUserDao(IUserDAO userDao) {
		this.userDao = userDao;
	}    
	
}
